package com.yunwushanhe.util;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class BeanCopyUtil {

    public static <T> T copyBean(Object source,Class<T> targetClazz){
        T result = null;
        try {
            result = targetClazz.newInstance();
            BeanUtils.copyProperties(source,result);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static <T,V> List<V> copyBeanList(List<T> source, Class<V> targetClazz){
        return source.stream()
                .map(object -> copyBean(object,targetClazz))
                .collect(Collectors.toList());
    }

    /**
     * 这个方法必须保证 updateContent 中的字段名称与 updateObject中的字段名称一致，否则会出问题
     */
    public static <T,V> UpdateWrapper<T> ignoreNullUpdateWrapper(Map<String,Object> eqMap, V updateContent, Class<T> updateObject){
        Map<String, Object> map =
                com.baomidou.mybatisplus.core.toolkit.BeanUtils.beanToMap(updateContent);
        map = new HashMap<>(map);
        UpdateWrapper<T> updateWrapper = new UpdateWrapper<>();

        Set<Map.Entry<String, Object>> eqEntrySet = eqMap.entrySet();
        for (Map.Entry<String, Object> entry : eqEntrySet) {
            map.remove(entry.getKey());
            updateWrapper.eq(camelToUnderline(entry.getKey()),entry.getValue());
        }

        Set<Map.Entry<String, Object>> updateEntrySet = map.entrySet();
        Iterator<Map.Entry<String, Object>> it = updateEntrySet.iterator();

        while(it.hasNext()){
            Map.Entry<String, Object> entry = it.next();
            updateWrapper.set(camelToUnderline(entry.getKey()),entry.getValue());
        }

        return updateWrapper;
    }

    /**
     * 这个方法必须保证 updateContent 中的字段名称与 updateObject中的字段名称一致，否则会出问题
     */
    public static <T,V> UpdateWrapper<T> ignoreNullUpdateWrapper(Map<String,Object> eqMap, V updateContent, Class<T> updateObject, List<String> ignoreFieldList){
        Map<String, Object> map =
                com.baomidou.mybatisplus.core.toolkit.BeanUtils.beanToMap(updateContent);
        map = new HashMap<>(map);
        UpdateWrapper<T> updateWrapper = new UpdateWrapper<>();

        Set<Map.Entry<String, Object>> eqEntrySet = eqMap.entrySet();
        for (Map.Entry<String, Object> entry : eqEntrySet) {
            updateWrapper.eq(camelToUnderline(entry.getKey()),entry.getValue());
            map.remove(entry.getKey());
        }

        for (String key : ignoreFieldList) {
            map.remove(key);
        }

        Set<Map.Entry<String, Object>> updateEntrySet = map.entrySet();
        Iterator<Map.Entry<String, Object>> it = updateEntrySet.iterator();

        while(it.hasNext()){
            Map.Entry<String, Object> entry = it.next();
            updateWrapper.set(camelToUnderline(entry.getKey()),entry.getValue());
        }

        return updateWrapper;
    }

    /**
     * 将驼峰转为下划线
     * @param fileName
     * @return java.lang.String
     */
    public static String camelToUnderline(String fileName) {
        //匹配 A-Z
        Pattern compile = Pattern.compile("[A-Z]");

        //进行匹配，结果存入 匹配器
        Matcher matcher = compile.matcher(fileName);
        Matcher matcherJudge = compile.matcher(String.valueOf(fileName.charAt(0)));
        StringBuffer sb = new StringBuffer();

        //如果匹配中存在
        while (matcher.find()) {
            //加入下换线，并且转为 小写。
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        //添加到 sb中
        matcher.appendTail(sb);

        if(matcherJudge.find()){
            //如果是首字符大写，直接把第一个 _ 去掉。
            return sb.toString().replaceFirst("_","");
        }
        return sb.toString();
    }

    public static HashMap<String,Object> eqMap(Object ... keyValue) throws Exception {
        if(keyValue.length % 2 == 1){
            throw new Exception("传入参数个数必须是偶数个");
        }
        HashMap<String, Object> eqMap = new HashMap<>();
        for (int i = 0; i < keyValue.length; i += 2) {
            eqMap.put((String) keyValue[i],keyValue[i+1]);
        }
        return eqMap;
    }

    public static ArrayList<String> ignoreFieldList(String ... argList){
        ArrayList<String> ignoreFieldList = new ArrayList<>();
        for (String key : argList) {
            ignoreFieldList.add(key);
        }
        return ignoreFieldList;
    }
}
